package com.aitemv.web.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aitemv.comm.ContainStatic;
import com.aitemv.entity.JsonBean;
import com.aitemv.entity.UserEntity;
import com.aitemv.services.UserService;
import com.aitemv.utils.CommUtil;
import com.aitemv.utils.PageBean;
import com.aitemv.web.base.BaseAction;

/**
 * 
 * @author lyvee 用户登陆action
 */
@Controller
public class UserAction<T> extends BaseAction {
	@Autowired
	private UserService userService;

	@Override
	@RequestMapping(value = "userCenter.htm", method = RequestMethod.GET)
	public String forward(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(false);// 获得session
		if (session != null) {
			if (session.getAttribute("userAcc") != null)// 判断是否登陆
				if (StringUtils.isEmpty(session.getAttribute("userAcc")
						.toString())) {
					return "userCenter";
				} else {
					return "aa";
				}

		}
		return "userCenter";
	}

	/** 查询所有的用户*** */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/back/userList.do", method = RequestMethod.GET)
	public @ResponseBody
	JsonBean getUserList(HttpServletRequest request) {
		String startPage = request.getParameter("start");// 获得前端传过来的参数
		String limit = request.getParameter("limit");
		String account = request.getParameter("account");// 查询条件account 和name
		String name = request.getParameter("name");
		List<Object> param = new ArrayList<Object>();
		Map<String,String> paramMap = new HashMap<String,String>();
		String whereStr = "";// sql where条件
		if (StringUtils.isNotEmpty(account)) {
			paramMap.put("account", ContainStatic.OPERATOR_LIKE_INC);
			param.add("%"+account+"%");
		}
		if (StringUtils.isNotEmpty(name)) {
			paramMap.put("name", ContainStatic.OPERATOR_LIKE_INC);
			param.add("%"+name+"%");
		}
		whereStr = CommUtil.conditSql(paramMap);
		int spage = 0;// 开始查询的记录
		int psize = 10;// 每页显示多少条
		int totalPage = userService.findUserCount();
		if (StringUtils.isNotEmpty(startPage)) {
			spage = Integer.parseInt(startPage);
		}
		if (StringUtils.isNotEmpty(limit)) {
			psize = Integer.parseInt(limit);
		}
		PageBean page = new PageBean(totalPage, spage, psize);// 构造page对象
		List<UserEntity> list = null;
		try {
			list = userService.findUserList(page, param,whereStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JsonBean bean = new JsonBean();
		bean.setTotalCount(totalPage);
		bean.setResult(list);
		return bean;
	}
}
